//
//  groupInfoViewController.m
//  QRMaster
//
//  Created by myApple on 15/10/12.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#define fontSize [UIFont systemFontOfSize:14.0]
#define viewHeight  50

#import "groupInfoViewController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "PromptInfoView.h"
#import "TouchBtnColor.h"
#import "RoomSeclectViewController.h"
#import "RequestNetWork.h"
#import "SetGuestDateTimeViewController.h"
#import "UIImageView+WebCache.h"
#import "DateSelectViewController.h"
#import "AppDelegate.h"
#import "validateInputValue.h"
#import "GroupGuestLogic.h"
#import "BackBtn.h"
#import "AreaChooseController.h"

@interface groupInfoViewController ()<ABPeoplePickerNavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate>
{
    
    UIImageView * TopBackGroundView;
    BOOL editFlag;//标记是编辑状态还不是编辑状态
    NSString * sexFlag;//当前性别
    BOOL isGroupManager;//yes是组管理员 no的话就不是组管理员
    AppDelegate * appDelegate;
    NSMutableArray * guestArray;
    
    //编辑前页面变量
    UILabel * groupNameTextLable;
    UILabel * groupManagerNameTextLabel;
    UILabel * groupManagerAccountTextLabel;
    UILabel * groupManagerEmailTextLabel;
    UILabel * groupManagerCertificateTextLabel;
    UILabel * sexLabel;
    UIScrollView * InfoScrollView;
    UIButton * roomBtn;
    UIButton * timeBtn;
    UIButton * callBtn;
    UIImageView * titleImgView;//个人头像
    
    //编辑页面变量
    UIImageView * editImgView;
    UIImageView * editedView;
    UIView * editBackView;
    UIView * editGroupManagerNameView;
    UIView * editGroupManagerCertificateView;
    UIView * editSexView;
    UITextField * groupNameTextField;
    UITextField * groupManagerNameTextField;
    UITextField * groupManagerCertificateTextField;
    UITextField * groupManagerAccountTextField;
    UIButton * manBtn;
    UIButton * womanBtn;
    UIButton * editingroomBtn;
    
    //支持国际版
    UIButton * mobileBtn;
    UIButton * emailBtn;
    UIButton * groupManagerAccountBtn;
    UIButton * mobileHeaderBtn;
    UILabel * emailHeaderLabel;
    UILabel * areaCodeIdLabel;
    UILabel * areaLabel;
}

@end

@implementation groupInfoViewController

-(void)setModel:(GuestModel *)model
{
    _model = model;
    groupNameTextLable.text = _groupName;
    groupManagerNameTextLabel.text = model.guestName;
     groupManagerEmailTextLabel.text = model.email;
    if (model.email == nil || [model.email isEqualToString:@""]) {
        if (![model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
            groupManagerAccountTextLabel.text = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.telePhone];
            groupManagerAccountTextField.text = model.telePhone;
            areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",model.areaCode];
            for (int i = 0 ; i < AreaCodeArray.count; i ++) {
                if ([model.areaCode isEqualToString:AreaCodeArray[i]]) {
                    areaLabel.text = AreaArray[i];
                }
            }
        }
        else
        {
            groupManagerAccountTextLabel.text = model.telePhone;
            areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",[QRSingleton sharedQRSingleton].currentAreaCodeString];
            groupManagerAccountTextField.text = groupManagerAccountTextLabel.text;
        }
        
    }
    else
    {
        groupManagerAccountTextField.text = model.email;
        [self emailClick];
    }
    groupManagerCertificateTextLabel.text = model.certificaties;
    sexLabel.text = [model.sex isEqualToString:@"1"]?NSLocalizedString(@"man", nil):NSLocalizedString(@"woman", nil);
    isGroupManager = [model.isGroupManager isEqualToString:@"1"]?YES:NO;
    groupNameTextField.text = groupNameTextLable.text;
    groupManagerCertificateTextField.text = groupManagerCertificateTextLabel.text;
    groupManagerNameTextField.text = groupManagerNameTextLabel.text;
    if ([model.isGroupManager isEqualToString:@"1"]) {
        [titleImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HEADERURL,model.headUrl]] placeholderImage:[UIImage imageNamed:@"96人像"]];
    }
    else
    {
        [titleImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",YSBHEADERIMGURL,model.headUrl]] placeholderImage:[UIImage imageNamed:@"96人像"]];
    }
    if ([model.sex isEqualToString:@"1"]) {
        manBtn.selected = YES;
        sexFlag = @"1";
    }
    else
    {
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
    if ([model.isGroupManager isEqualToString:@"0"]) {
        editGroupManagerNameView.frame = CGRectMake(10, 5, editGroupManagerNameView.frame.size.width, viewHeight);
        editGroupManagerCertificateView.frame = CGRectMake(10, editGroupManagerNameView.frame.origin.y + viewHeight + 5, editGroupManagerCertificateView.frame.size.width, viewHeight);
        editSexView.frame = CGRectMake(10, editGroupManagerCertificateView.frame.origin.y + viewHeight + 5, editSexView.frame.size.width, viewHeight);
    }
    if ([groupManagerAccountTextLabel.text isEqualToString:@""] || groupManagerAccountTextLabel.text == nil) {
        callBtn.hidden = YES;
    }
}

-(void)setIsGroup:(BOOL)isGroup
{
    _isGroup = isGroup;
}

#pragma mark -------- 是否有编辑组管理员或者组内客户的权限
-(void)setManagerRole:(NSString *)managerRole
{
    _managerRole = managerRole;
    if (![_managerRole isEqualToString:@"1"] && [_model.isGroupManager isEqualToString:@"0"]) {
        editBackView.hidden = YES;
        roomBtn.hidden = YES;
        timeBtn.hidden = YES;
        InfoScrollView.contentSize = CGSizeMake(0, roomBtn.frame.origin.y);
    }
}

-(id)init
{
    self = [super init];
    if (self) {
        appDelegate = [UIApplication sharedApplication].delegate;
        guestArray = [[NSMutableArray alloc]init];
        [self prepareHeadView];
        [self prepareInfoView];
        [self prepareEditingView];
    }
    return  self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
    editFlag = YES;
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    sexFlag = @"0";
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
     self.navigationController.navigationBarHidden = NO;
}


-(void)prepareHeadView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = 64;
    
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    topView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:topView];
    
    y = 0;
    width = ScreenWidth;
    height = ScreenHeight / 3.0;
    TopBackGroundView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    TopBackGroundView.userInteractionEnabled = YES;
    TopBackGroundView.image = [UIImage imageNamed:@"个人背景"];
    [self.view addSubview:TopBackGroundView];
    
    
    UIView * alpBackView =[[UIView alloc]initWithFrame:topView.frame];
    alpBackView.backgroundColor =[UIColor clearColor];
    [self.view addSubview:alpBackView];
    
    x = 16;
    y = 20;
    height = 40;
    width = 40;
    UIButton * backBtn = [BackBtn initWithFrame:CGRectMake(x, y, width, height) withImage:@"navigationBack"];
    [backBtn addTarget:self action:@selector(BackClick) forControlEvents:UIControlEventTouchUpInside];
    [alpBackView addSubview:backBtn];
    
    //编辑
    x = ScreenWidth - 60;
    width = 40;
    y = 20;
    height = 40;
    editBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [alpBackView addSubview:editBackView];
    x = 10;
    y = 10;
    width = 20;
    height = 20;
    editImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editImgView.image = [UIImage imageNamed:@"ic_edit"];
    editImgView.userInteractionEnabled = YES;
    [editBackView addSubview:editImgView];
    UITapGestureRecognizer * editTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editClick)];
    [editBackView addGestureRecognizer:editTap];
}


#pragma mark -- 点击编辑按钮
-(void)editClick
{
    if (editFlag) {
        editImgView.image = [UIImage imageNamed:@"ic_save"];
        CGRect ImgFrame = TopBackGroundView.frame;
        ImgFrame.size.height = 0;
        CGRect HeadImgFrame = titleImgView.frame;
        HeadImgFrame.origin.y = 34;
        [UIView animateWithDuration:0.3 animations:^{
            TopBackGroundView.frame = ImgFrame;
            titleImgView.frame = HeadImgFrame;
            titleImgView.alpha = 0;
        } completion:^(BOOL finished) {
            InfoScrollView.hidden = YES;
            editedView.hidden = NO;
        }];
    }
    else
    {
        [self saveClick];
    }
    editFlag = !editFlag;
}


-(void)prepareInfoView
{
    float ScreenWidth = self.view.frame.size.width;
    float ScreenHeight = self.view.frame.size.height;
    float x = 0;
    float y = TopBackGroundView.frame.size.height;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    InfoScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    InfoScrollView.showsVerticalScrollIndicator = YES;
    InfoScrollView.showsHorizontalScrollIndicator = NO;
    InfoScrollView.backgroundColor = RGBA(237, 238, 241, 1);
    [self.view addSubview:InfoScrollView];
    
    //姓名
    x = 0;
    y = 0;
    width = ScreenWidth;
    height = viewHeight;
    UIView * groupNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:groupNameView];
    
    x = 30;
    y = 25;
    width = groupNameView.frame.size.width - x ;
    height = 30;
    groupManagerNameTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerNameTextLabel.font = [UIFont systemFontOfSize:14.0];
    [groupNameView addSubview:groupManagerNameTextLabel];
    
//    x = 20;
    y = TopBackGroundView.frame.size.height - 30;
    width = 50;
    height = 50;
    titleImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    titleImgView.layer.cornerRadius = 25;
    titleImgView.layer.masksToBounds =YES;
    [self.view addSubview:titleImgView];
    
    //手机号码
    x = 10;
    y = 5 + groupNameView.frame.size.height + groupNameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerAccountView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerAccountView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:groupManagerAccountView];
    
    x = 10;
    y = 15;
    width = 80;
    height = 20;
    UILabel * groupManagerAccountLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerAccountLabel.text = NSLocalizedString(@"editTelePhone", nil);
    groupManagerAccountLabel.font = fontSize;
    [groupManagerAccountView addSubview:groupManagerAccountLabel];
    
    x = 100;
    y = 10;
    width = groupManagerAccountView.frame.size.width - 60 ;
    height = 30;
    groupManagerAccountTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerAccountTextLabel.font = [UIFont systemFontOfSize:14.0];
    [groupManagerAccountView addSubview:groupManagerAccountTextLabel];
    
    x = groupManagerAccountView.frame.size.width - 50;
    y = 0;
    height = 50;
    width = 50;
    callBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    callBtn.frame = CGRectMake(x, y, width, height);
    [callBtn setBackgroundColor:RGBA(125, 194, 52, 1)];
    [callBtn addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];
    [groupManagerAccountView addSubview:callBtn];
    
    x = 15;
    y = 15;
    width = 20;
    height = 20;
    UIImageView * callImg = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    callImg.image = [UIImage imageNamed:@"ic_call30"];
    [callBtn addSubview:callImg];
    
    //邮箱
    x = 10;
    y = 5 + groupManagerAccountView.frame.size.height + groupManagerAccountView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerEmailView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerEmailView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:groupManagerEmailView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * groupManagerEmailLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerEmailLabel.text = NSLocalizedString(@"mineEmail", nil);
    groupManagerEmailLabel.font = fontSize;
    [groupManagerEmailView addSubview:groupManagerEmailLabel];
    
    x = 100;
    y = 10;
    width = groupManagerAccountView.frame.size.width - 60 ;
    height = 30;
    groupManagerEmailTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerEmailTextLabel.font = [UIFont systemFontOfSize:14.0];
    [groupManagerEmailView addSubview:groupManagerEmailTextLabel];
    
    //组管理员姓名
    x = 10;
    y = 5 + groupManagerEmailView.frame.size.height + groupManagerEmailView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerNameView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:groupManagerNameView];
    
    x = 10;
    y = 15;
    width = 90;
    height = 20;
    UILabel * groupNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameLabel.text = NSLocalizedString(@"groupName", nil);
    groupNameLabel.font = fontSize;
    [groupManagerNameView addSubview:groupNameLabel];
    
    x = 100;
    y = 10;
    width = groupManagerNameView.frame.size.width - x ;
    height = 30;
    groupNameTextLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameTextLable.font = [UIFont systemFontOfSize:14.0];
    [groupManagerNameView addSubview:groupNameTextLable];
    
    //身份证
    x = 10;
    y = 5 + groupManagerNameView.frame.size.height + groupManagerNameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerCertificateView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:groupManagerCertificateView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * groupManagerCertificateLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateLabel.text = NSLocalizedString(@"certificate", nil);
    groupManagerCertificateLabel.font = fontSize;
    [groupManagerCertificateView addSubview:groupManagerCertificateLabel];
    
    x = 100;
    y = 10;
    width = groupManagerCertificateView.frame.size.width - x ;
    height = 30;
    groupManagerCertificateTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateTextLabel.font = [UIFont systemFontOfSize:14.0];
    [groupManagerCertificateView addSubview:groupManagerCertificateTextLabel];
    
    //性别
    x = 10;
    y = 5 + groupManagerCertificateView.frame.size.height + groupManagerCertificateView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:sexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = fontSize;
    [sexView addSubview:editSexLabel];
    
    x = 100;
    width = ScreenWidth - x;
    sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexLabel.font = fontSize;
    sexLabel.textColor = RGBA(143, 147, 148, 1);
    [sexView addSubview:sexLabel];

    
    //开门权限
    x = 10;
    y = sexView.frame.size.height + sexView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    roomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    roomBtn.frame = CGRectMake(x, y, width, height);
    roomBtn.backgroundColor = RGBA(248, 248, 248, 1);
    [roomBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:roomBtn.frame.size] forState:UIControlStateHighlighted];
    [roomBtn addTarget:self action:@selector(roomSelect) forControlEvents:UIControlEventTouchUpInside];
    [InfoScrollView addSubview:roomBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * roomLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomLabel.text = NSLocalizedString(@"openRoomPower", nil);
    roomLabel.font = [UIFont systemFontOfSize:14.0];
    [roomBtn addSubview:roomLabel];
    
    x = roomBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * roomImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomImgView.image = [UIImage imageNamed:@"36右箭头"];
    [roomBtn addSubview:roomImgView];
    
    //分配开门时间
    x = 10;
    y = roomBtn.frame.size.height + roomBtn.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    timeBtn.frame = CGRectMake(x, y, width, height);
    timeBtn.backgroundColor = RGBA(248, 248, 248, 1);
    [timeBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:timeBtn.frame.size] forState:UIControlStateHighlighted];
    [timeBtn addTarget:self action:@selector(dateTimeSelect) forControlEvents:UIControlEventTouchUpInside];
    [InfoScrollView addSubview:timeBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeLabel.text = NSLocalizedString(@"setOpenTime", nil);
    timeLabel.font = [UIFont systemFontOfSize:14.0];
    [timeBtn addSubview:timeLabel];
    
    x = timeBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * timeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeImgView.image = [UIImage imageNamed:@"36右箭头"];
    [timeBtn addSubview:timeImgView];
    
    InfoScrollView.contentSize = CGSizeMake(0, timeBtn.frame.origin.y + timeBtn.frame.size.height);
}

#pragma mark -- 点击拨打手机

-(void)callPhone
{
    NSString * callString = nil;
    if (![_model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
        callString = [NSString stringWithFormat:@"+%@-%@",_model.areaCode,_model.telePhone];
    }
    else
    {
        callString = groupManagerAccountTextField.text;
    }

    NSString *telUrl = [NSString stringWithFormat:@"telprompt://%@",callString];
    NSURL *url = [NSURL URLWithString:telUrl];
    [[UIApplication sharedApplication] openURL:url];
}


#pragma mark ---------- 编辑页面
-(void)prepareEditingView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 64;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    editedView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editedView.hidden = YES;
    editedView.backgroundColor =RGBA(237, 238, 241, 1);
    editedView.userInteractionEnabled = YES;
    [self.view addSubview:editedView];
    
    //类型
    x = 10;
    y = 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * typeView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeView.backgroundColor = RGBA(248, 248, 248, 1);
    [editedView addSubview:typeView];
    
    UILabel * typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeLabel.text = NSLocalizedString(@"type", nil);
    typeLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:typeLabel];
    
    // mobile
    x = 90;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * mobileView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:mobileView];
    mobileView.userInteractionEnabled = YES;
    UITapGestureRecognizer * mobileTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mobileClick)];
    [mobileView addGestureRecognizer:mobileTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    mobileBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [mobileBtn addTarget:self action:@selector(mobileClick) forControlEvents:UIControlEventTouchUpInside];
    mobileBtn.selected = YES;
    [mobileView addSubview:mobileBtn];
    
    x = 120;
    y = 20;
    width = 60;
    height = 20;
    UILabel * mobileLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    mobileLabel.text =  NSLocalizedString(@"loginPromt", nil);
    mobileLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:mobileLabel];
    
    //email
    x = 180;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * emailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:emailImageView];
    emailImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * emailTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emailClick)];
    [emailImageView addGestureRecognizer:emailTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    emailBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [emailBtn addTarget:self action:@selector(emailClick) forControlEvents:UIControlEventTouchUpInside];
    [emailImageView addSubview:emailBtn];
    
    x = 220;
    height = 20;
    y = 20;
    UILabel * emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    emailLabel.text =  NSLocalizedString(@"mineEmail", nil);
    emailLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:emailLabel];
    
    //组名
    x = 10;
    y = typeView.frame.size.height + typeView.frame.origin.y + 5;
    width = ScreenWidth - x - 5 ;
    height = viewHeight;
    UIView * groupNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameView.backgroundColor = RGBA(248, 248, 248, 1);
    [editedView addSubview:groupNameView];
    
    x = 10;
    y = 15;
    width = 90;
    height = 20;
    UILabel * groupNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameLabel.text = NSLocalizedString(@"groupName", nil);
    groupNameLabel.font = fontSize;
    [groupNameView addSubview:groupNameLabel];
    
    x = 100;
    y = 10;
    width = groupNameView.frame.size.width - x ;
    height = 30;
    groupNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameTextField.font = [UIFont systemFontOfSize:14.0];
    groupNameTextField.borderStyle = UITextBorderStyleNone;
    groupNameTextField.delegate  = self;
    [groupNameView addSubview:groupNameTextField];
    
    //组管理员账号
    x = 10;
    y = 5 + groupNameView.frame.size.height + groupNameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerAccountView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerAccountView.backgroundColor = RGBA(248, 248, 248, 1);
    [editedView addSubview:groupManagerAccountView];
    
    x = 100;
    y = 10;
    width = groupManagerAccountView.frame.size.width - x - 50 ;
    height = 30;
    groupManagerAccountTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerAccountTextField.font = [UIFont systemFontOfSize:14.0];
    groupManagerAccountTextField.borderStyle = UITextBorderStyleNone;
    groupManagerAccountTextField.placeholder = NSLocalizedString(@"account", nil);
    [groupManagerAccountView addSubview:groupManagerAccountTextField];
    
    //前面点击的区号按钮
    x = 10;
    y = 5;
    width = 40;
    mobileHeaderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    mobileHeaderBtn.frame = CGRectMake(x, y, width, height);
    [mobileHeaderBtn addTarget:self action:@selector(chooseArea) forControlEvents:UIControlEventTouchUpInside];
    [groupManagerAccountView addSubview:mobileHeaderBtn];
    
    areaCodeIdLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 60, 15)];
    areaCodeIdLabel.font = [UIFont systemFontOfSize:10.0];
    areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",[QRSingleton sharedQRSingleton].currentAreaCodeString];
    [mobileHeaderBtn addSubview:areaCodeIdLabel];
    
    areaLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, 60, 15)];
    areaLabel.font = [UIFont systemFontOfSize:10.0];
    areaLabel.text = [QRSingleton sharedQRSingleton].currentAreaString;
    [mobileHeaderBtn addSubview:areaLabel];
    
    emailHeaderLabel = [[UILabel alloc]initWithFrame:CGRectMake(mobileHeaderBtn.frame.origin.x, mobileHeaderBtn.frame.origin.y + 5, mobileHeaderBtn.frame.size.width, mobileHeaderBtn.frame.size.height)];
    emailHeaderLabel.text = NSLocalizedString(@"mineEmail", nil);
    emailHeaderLabel.hidden = YES;
    emailHeaderLabel.font = [UIFont systemFontOfSize:14.0];
    [groupManagerAccountView addSubview:emailHeaderLabel];
    
    x = groupManagerAccountView.frame.size.width - 50;
    y = 0;
    width = 50;
    height = 50;
    groupManagerAccountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    groupManagerAccountBtn.frame = CGRectMake(x, y, width, height);
    [groupManagerAccountBtn setBackgroundColor:[UIColor greenColor]];
    [groupManagerAccountBtn setBackgroundImage:nil forState:0];
    [groupManagerAccountBtn addTarget:self action:@selector(addTelePhone) forControlEvents:UIControlEventTouchUpInside];
    [groupManagerAccountView addSubview:groupManagerAccountBtn];
    
    UIImageView * adderssImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10,10, 30, 30)];
    adderssImgView.image = [UIImage imageNamed:@"ic_txl"];
    [groupManagerAccountBtn addSubview:adderssImgView];

    
    //组管理员姓名
    x = 10;
    y = 5 + groupManagerAccountView.frame.size.height + groupManagerAccountView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    editGroupManagerNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editGroupManagerNameView.backgroundColor = RGBA(248, 248, 248, 1);
    [editedView addSubview:editGroupManagerNameView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * groupManagerNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerNameLabel.text = NSLocalizedString(@"userName", nil);
    groupManagerNameLabel.font = fontSize;
    [editGroupManagerNameView addSubview:groupManagerNameLabel];
    
    x = 100;
    y = 10;
    width = editGroupManagerNameView.frame.size.width - x ;
    height = 30;
    groupManagerNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerNameTextField.font = [UIFont systemFontOfSize:14.0];
    groupManagerNameTextField.borderStyle = UITextBorderStyleNone;
    groupManagerNameTextField.delegate = self;
    [editGroupManagerNameView addSubview:groupManagerNameTextField];
    
    //身份证
    x = 10;
    y = 5 + editGroupManagerNameView.frame.size.height + editGroupManagerNameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    editGroupManagerCertificateView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editGroupManagerCertificateView.backgroundColor = RGBA(248, 248, 248, 1);
    [editedView addSubview:editGroupManagerCertificateView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * groupManagerCertificateLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateLabel.text = NSLocalizedString(@"certificate", nil);
    groupManagerCertificateLabel.font = fontSize;
    [editGroupManagerCertificateView addSubview:groupManagerCertificateLabel];
    
    x = 100;
    y = 10;
    width = editGroupManagerCertificateView.frame.size.width - x ;
    height = 30;
    groupManagerCertificateTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateTextField.font = [UIFont systemFontOfSize:14.0];
    groupManagerCertificateTextField.borderStyle = UITextBorderStyleNone;
    groupManagerCertificateTextField.delegate = self;
    [editGroupManagerCertificateView addSubview:groupManagerCertificateTextField];
    
    //性别
    x = 10;
    y = 5 + editGroupManagerCertificateView.frame.size.height + editGroupManagerCertificateView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    editSexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexView.backgroundColor = RGBA(248, 248, 248, 1);
    [editedView addSubview:editSexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = fontSize;
    [editSexView addSubview:editSexLabel];
    
    x = 100;
    y = 0;
    width = 40;
    height = 50;
    UIImageView * manView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [editSexView addSubview:manView];
    manView.userInteractionEnabled = YES;
    UITapGestureRecognizer * manTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(manClick)];
    [manView addGestureRecognizer:manTap];
    
    x = 100;
    y = 15;
    width = 20;
    height = 20;
    manBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [manBtn addTarget:self action:@selector(manClick) forControlEvents:UIControlEventTouchUpInside];
    [editSexView addSubview:manBtn];
    
    x = 140;
    height = 20;
    UILabel * manLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 40, height)];
    manLabel.text = NSLocalizedString(@"man", nil);
    manLabel.font = fontSize;
    [editSexView addSubview:manLabel];
    
    manBtn.selected = YES;
    
    x = 180;
    y = 0;
    width = 40;
    height = viewHeight;
    UIImageView * womanView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [editSexView addSubview:womanView];
    womanView.userInteractionEnabled = YES;
    UITapGestureRecognizer * womanTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(womanClick)];
    [womanView addGestureRecognizer:womanTap];
    
    x += 10;
    y += 15;
    width = 20;
    height = 20;
    womanBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [womanBtn addTarget:self action:@selector(womanClick) forControlEvents:UIControlEventTouchUpInside];
    [editSexView addSubview:womanBtn];
    
    x = 220;
    height = 20;
    UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    womanLabel.text = NSLocalizedString(@"woman", nil);
    womanLabel.font = fontSize;
    [editSexView addSubview:womanLabel];
    
}

#pragma mark ----------点击手机号
-(void)mobileClick
{
    mobileBtn.selected = !mobileBtn.selected;
    if (mobileBtn.selected) {
        emailBtn.selected = NO;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        groupManagerAccountBtn.hidden = NO;
    }
    else
    {
        emailBtn.selected = YES;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        groupManagerAccountBtn.hidden = YES;
    }
}


#pragma mark ----------点击邮箱
-(void)emailClick
{
    emailBtn.selected = !emailBtn.selected;
    if (emailBtn.selected) {
        mobileBtn.selected = NO;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        groupManagerAccountBtn.hidden = YES;
    }
    else
    {
        mobileBtn.selected = YES;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        groupManagerAccountBtn.hidden = NO;
    }
}


#pragma mark ---------- 改变地区或者国家
-(void)chooseArea
{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    acc.areaCode = ^(NSString * areaCodeString,NSString * areaString)
    {
        areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",areaCodeString];
        areaLabel.text = areaString;
    };
}

#pragma mark -- 从通讯录添加职员
-(void)addTelePhone
{
    ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentViewController:peoplePicker animated:NO completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    groupManagerNameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        groupManagerAccountTextField.text = [phones objectAtIndex:0];
        groupManagerAccountTextField.text = [groupManagerAccountTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

//2_0到8_0系统使用的方法
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    groupManagerNameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        groupManagerAccountTextField.text = [phones objectAtIndex:0];
        groupManagerAccountTextField.text = [groupManagerAccountTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return NO;
}

#pragma mark --- uitextfieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self hiddenKeyboard];
    return YES;
}


-(void)manClick
{
    if (manBtn.selected) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
        
    }
    else
    {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
        
    }
}

-(void)womanClick
{
    if (womanBtn.selected) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
        
        
    }
    else
    {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
        
    }
}

-(void)saveClick
{
    [self hiddenKeyboard];
    if (![self validateInputValue]) {
        return;
    }
    if ([groupManagerAccountTextField.text isEqualToString:groupManagerEmailTextLabel.text]||[groupManagerAccountTextField.text isEqualToString:groupManagerAccountTextLabel.text]) {
        [self makeSureSave];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"feedBackAlterPromt", nil) message: NSLocalizedString(@"changeGroupManager", nil)  delegate:self cancelButtonTitle:NSLocalizedString(@"alterCancel", nil) otherButtonTitles:NSLocalizedString(@"alterSure",nil), nil];
        [alert show];
    }
}

#pragma mark -- AlterViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self makeSureSave];
    }
}

#pragma mark -- saveEditInfo
-(void)makeSureSave
{
    if ([self IsHaveNetwork]) {
        return;
    }
    NSString *paramStr=@"";
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:_groupId forKey:@"groupId"];
    [postDict setValue:groupNameTextField.text forKey:@"groupName"];
    [postDict setValue:groupManagerNameTextField.text forKey:@"name"];
    [postDict setValue:groupManagerCertificateTextField.text forKey:@"identity"];
    [postDict setValue:sexFlag forKey:@"gender"];
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    if (_isGroup) {
        if (mobileBtn.selected) {
            if (![validateInputValue isPureNumandCharacters:groupManagerAccountTextField.text]) {
                [PromptInfoView showMessage:NSLocalizedString(@"accountError", nil) superView:self.view];
                return;
            }
            [postDict setValue:[areaCodeIdLabel.text substringFromIndex:1] forKey:@"areaCode"];
        }
        else
        {
            if (![validateInputValue validateEmail:groupManagerAccountTextField.text]) {
                [PromptInfoView showMessage:NSLocalizedString(@"accountError", nil) superView:self.view];
                return;
            }
        }
        [postDict setValue:_model.guestid forKey:@"groupManagerId"];
        [postDict setValue:groupManagerAccountTextField.text forKey:@"account"];
        
        if([self getTimestamp:_groupId] != nil)
         {
            [postDict setValue:[self getTimestamp:_groupId] forKey:@"timestamp"];
         }
        else
         {
            [postDict setValue:@"" forKey:@"timestamp"];
         }
        paramStr = POST(EDITGROUPMANAGERINFO);
        
    }else
    {
        if (groupManagerAccountTextLabel.text == nil || [groupManagerAccountTextLabel.text isKindOfClass:[NSNull class]]) {
            groupManagerAccountTextLabel.text = @"";
        }
        [postDict setValue:_model.areaCode forKey:@"areaCode"];
        [postDict setValue:_model.telePhone forKey:@"mobile"];
        [postDict setValue:groupManagerEmailTextLabel.text forKey:@"email"];

        if([self getTimestamp:_groupId] != nil)
        {
            [postDict setValue:[self getTimestamp:_groupId] forKey:@"timestamp"];
        }
        else
        {
            [postDict setValue:@"" forKey:@"timestamp"];
        }
        paramStr = POST(EDITGROUPCLENTINFO);
    }
    [self startWaiting:nil color:nil addToView:self.view];
    [rnw initWithPath:paramStr params:postDict block:^(id downLoadData, id error)
     {
         [self stopWaiting];
         if([downLoadData objectForKey:@"cache"] != nil){
             GroupGuestLogic  *groupGuestLogic = [[GroupGuestLogic alloc]init];
             [groupGuestLogic deleteGroupGuestInfo:_groupId groupGuest:_model.guestid];
             if([[[downLoadData objectForKey:@"cache"] objectForKey:@"status"]integerValue] == 0)
             {
                 if (!_isGroup) {
                     [self updateTimestamp:_groupId  timeKeyValue:[[downLoadData objectForKey:@"cache"] objectForKey:@"timestamp"]];
                 }
                 
             }
             NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
             data = [downLoadData objectForKey:@"data"];
             if (_isGroup) {
                 if ([[downLoadData objectForKey:@"data"] objectForKey:@"groupManagerId"]) {
                     _model.guestid = [data objectForKey:@"groupManagerId"];
                 }
                 _model.email = [data objectForKey:@"email"];
                 _model.firstChar = [data objectForKey:@"firstchar"];
                 _model.sex = [data objectForKey:@"gender"];
                 _model.headUrl = [data objectForKey:@"headurl"];
                 _model.guestid = [data objectForKey:@"id"];
                 _model.certificaties = [data objectForKey:@"identity"];
                 _model.telePhone = [data objectForKey:@"mobile"];
                 _model.guestName = [data objectForKey:@"name"];
                 if (mobileBtn.selected) {
                     _model.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
                 }
                 else
                 {
                     _model.areaCode = nil;
                 }
                 [titleImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HEADERURL,_model.headUrl]] placeholderImage:[UIImage imageNamed:@"96人像"]];
             }
             else
             {
                 _model.sex = sexFlag;
                 _model.certificaties = groupManagerCertificateTextField.text;
                 _model.guestName = groupManagerNameTextField.text;
             }
             [guestArray removeAllObjects];
             [guestArray addObject:_model];
//             __weak groupInfoViewController * weakSelf = self;
//             [groupGuestLogic queryGroupGuestInfo:_groupId block:^(NSMutableArray *groupGuestArray) {
//                 for (NSDictionary * dict in groupGuestArray)
//                 {
//                     if ([groupManagerAccountTextField.text isEqualToString:[dict objectForKey:@"email"]]||[groupManagerAccountTextField.text isEqualToString:[dict objectForKey:@"mobile"]]) {
//                         [weakSelf deleteSqliteGuest:[dict objectForKey:@"id"]];
//                         break;
//                     }
//                 }
//             }];
             [groupGuestLogic insertGroupGuestInfo:_model groupName:groupNameTextField.text groupStartTime:[QRSingleton sharedQRSingleton].groupBeginTime groupEndTime:[QRSingleton sharedQRSingleton].groupEndTime groupId:_groupId];
             
             [PromptInfoView showMessage:NSLocalizedString(@"editSuccess", nil) superView:self.view];
             editImgView.image = [UIImage imageNamed:@"ic_edit"];
             InfoScrollView.hidden = NO;
             editedView.hidden = YES;
             CGRect frame = InfoScrollView.frame;
             CGRect ImgFrame = TopBackGroundView.frame;
             frame.origin.y = [UIScreen mainScreen].bounds.size.height / 3.0;
             ImgFrame.size.height = [UIScreen mainScreen].bounds.size.height / 3.0;
             CGRect HeadImgFrame = titleImgView.frame;
             HeadImgFrame.origin.y = [UIScreen mainScreen].bounds.size.height / 3.0 - 30;
             [UIView animateWithDuration:0.3 animations:^{
                 InfoScrollView.frame = frame;
                 TopBackGroundView.frame = ImgFrame;
                 titleImgView.frame = HeadImgFrame;
                 titleImgView.alpha = 1.0;
             } completion:^(BOOL finished) {
                 InfoScrollView.scrollEnabled = YES;
             }];
             groupNameTextLable.text = groupNameTextField.text;
             groupManagerCertificateTextLabel.text = groupManagerCertificateTextField.text;
             _model.certificaties = groupManagerCertificateTextField.text;
             sexLabel.text = [sexFlag isEqualToString:@"1"]?NSLocalizedString(@"man", nil):NSLocalizedString(@"woman", nil);
             _model.sex = sexFlag;
             groupManagerNameTextLabel.text = groupManagerNameTextField.text;
             _model.guestName = groupManagerNameTextField.text;
         }
         
         if ([validateInputValue isPureNumandCharacters:groupManagerAccountTextField.text])
         {
             if (![[areaCodeIdLabel.text substringFromIndex:1]isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                 groupManagerAccountTextLabel.text = [NSString stringWithFormat:@"+%@ %@",[areaCodeIdLabel.text substringFromIndex:1],groupManagerAccountTextField.text];
             }
             else
             {
                 groupManagerAccountTextLabel.text = groupManagerAccountTextField.text;
             }
             groupManagerEmailTextLabel.text = @"";
         }
         else
         {
             groupManagerEmailTextLabel.text = groupManagerAccountTextField.text;
             groupManagerAccountTextLabel.text = @"";
         }
         _model.telePhone = groupManagerAccountTextLabel.text;
         _model.email = groupManagerEmailTextLabel.text;
         if (error) {
             [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
         }
         if ([groupManagerAccountTextLabel.text isEqualToString:@""] || groupManagerAccountTextLabel.text == nil) {
             callBtn.hidden = YES;
         }
         else
         {
             callBtn.hidden = NO;
         }

     }
     
     ];
}

//-(void)deleteSqliteGuest:(NSString *)guestId
//{
//    GroupGuestLogic * groupGuestLogic = [[GroupGuestLogic alloc]init];
//    [groupGuestLogic deleteGroupGuestInfo:_groupId groupGuest:guestId];
//}

-(void)roomSelect
{
    RoomSeclectViewController * rsvc = [[RoomSeclectViewController alloc]init];
    rsvc.communityid = _communityId;
    rsvc.email = groupManagerEmailTextLabel.text;
    rsvc.mobile = groupManagerAccountTextField.text;
    rsvc.guestOrStaff = @"2";
    rsvc.groupId = _groupId;
    rsvc.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
    [self.navigationController pushViewController:rsvc animated:YES];
}

-(void)dateTimeSelect
{
    if (isGroupManager) {
        DateSelectViewController * dsvc = [[DateSelectViewController alloc]init];
        dsvc.guestId = _model.guestid;
        dsvc.communityId = _communityId;
        dsvc.groupId = _groupId;
        dsvc.beginTime = [QRSingleton sharedQRSingleton].groupBeginTime;
        dsvc.endTime = [QRSingleton sharedQRSingleton].groupEndTime;
        [self.navigationController pushViewController:dsvc animated:YES];
    }
    else
    {
        SetGuestDateTimeViewController * sgdtvc = [[SetGuestDateTimeViewController alloc]init];
        sgdtvc.guestId = _model.guestid;
        sgdtvc.community = _communityId;
        [self.navigationController pushViewController:sgdtvc animated:YES];
    }
    
}

#pragma mark -- 提交数据之前的判断
-(BOOL)validateInputValue
{
    if (groupNameTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"errorGroupName", nil)];
        return NO;
    }
    if (groupManagerAccountTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"errorAccount", nil)];
        return NO;
    }
    if(![validateInputValue isPureNumandCharacters:groupManagerAccountTextField.text] && ![validateInputValue validateEmail:groupManagerAccountTextField.text])
    {
        [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
        return NO;
    }
    
    if (groupManagerNameTextField.text.length == 0 ||groupManagerAccountTextField.text.length > 40)
    {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil)];
        return NO;
    }
    return YES;
}


-(void)BackClick
{
    //从添加组跳转
    if([@"0" isEqualToString:[QRSingleton sharedQRSingleton].groupPush] == YES )
    {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


-(void)hiddenKeyboard
{
    [groupNameTextField resignFirstResponder];
    [groupManagerNameTextField resignFirstResponder];
    [groupManagerCertificateTextField resignFirstResponder];
    [groupManagerAccountTextField resignFirstResponder];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//通过groupId来查询到时间戳
-(NSString *)getTimestamp:(NSString *)groupId
{
    GroupGuestLogic *groupGuestLogic = [[GroupGuestLogic alloc]init];
    return [groupGuestLogic queryGroupGuestTime:groupId];
}


//通过groupId来更新时间戳
-(void)updateTimestamp:(NSString *)groupId timeKeyValue:(NSString *)timeKeyValue
{
    GroupGuestLogic *groupGuestLogic = [[GroupGuestLogic alloc]init];
    [groupGuestLogic updateGroupGuestTime:groupId keyValue:timeKeyValue];
}

@end
